//
//  InviteReexamineViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/19.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "InviteReexamineViewController.h"
#import "MineInfoEditViewController.h"
#import "InterviewBaseinfoModel.h"
#import "InviteReexamineSelectJobViewController.h"

@interface InviteReexamineViewController ()<TimeSelectPickerViewDelegate, PSSingleRowViewDelegate, MineInfoEditViewControllerDelegate>
{
    NSMutableArray *timeDataArray;
    InterviewBaseinfoModel *interviewBaseinfoModel;
    
    UIScrollView *bottomScrollView;
    
    PSCell *dateTimeCell;
    PSCell *timeCell;
    PSCell *contactCell;
    PSCell *phoneCell;
    PSCell *addressCell;
    PSCell *notesCell;
    UISwitch *switchBtn;
    UILabel *postContentLbl;
}
@property (nonatomic, strong) TimeSelectPickerView *dateTimePickerView;
@property (nonatomic, strong) PSSingleRowView *timePickerView;
@end

@implementation InviteReexamineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    if (self.dateTimePickerView) {
        self.dateTimePickerView.hidden = YES;
    }
    if (self.timePickerView) {
        self.timePickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    interviewBaseinfoModel = [[InterviewBaseinfoModel alloc] init];
    [self loadDataForTime];
    [self getInterviewBaseinfo];
//    [self initUI];
}
- (void)getInterviewBaseinfo {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:self.user_id forKey:@"userid"];
    
    [BaseNetManager requestWithMethod:POST path:Company_GetInterviewBaseinfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            [self->interviewBaseinfoModel setValuesForKeysWithDictionary:data];
            SVPHUDDismiss;
        }
        [self initUI];
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)initUI {
    __weak __typeof(self)weakSelf = self;
    self.dateTimePickerView = [[TimeSelectPickerView alloc] initWithTarget:self time:nil timeSelectPickerType:TimeSelectPickerTypeOfDefault dateFormat:nil];
    self.dateTimePickerView.hidden = YES;
    self.timePickerView = [[PSSingleRowView alloc] initWithData:timeDataArray target:self];
    self.timePickerView.hidden = YES;
    
    bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SIMEmojKeyBoardkBottomNOSafeArea_Height-HeightOfStatusBarAndNavigationBar)];
    [self.view addSubview:bottomScrollView];
    
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(30*Scale, 0, WIDTH-60*Scale, 50*Scale) text:[NSString stringWithFormat:@"与 %@ 的复试邀约", ((interviewBaseinfoModel.name&&interviewBaseinfoModel.name.length)?interviewBaseinfoModel.name:((self.user_name&&self.user_name.length)?self.user_name:@""))] textColor:THEMECOLOR_OF_TEXT_333333 font:Font_27];
    [bottomScrollView addSubview:topLbl];
    
    CGFloat postWidth = [UIToolsControl textWidth:@"面试岗位：" size:CGSizeMake(MAXFLOAT, 40*Scale) font:Font_18]+1;
    UILabel *postLbl = [UIToolsControl initLabelWithFrame:CGRectMake(30*Scale, EndY(topLbl), postWidth, 40*Scale) text:@"面试岗位：" textColor:THEMECOLOR_OF_TEXT_666666 font:Font_18];
    [bottomScrollView addSubview:postLbl];
    InterviewBaseinfoJobModel *tempModel = [[InterviewBaseinfoJobModel alloc] init];
    if (interviewBaseinfoModel.job_list.count) {
        tempModel = interviewBaseinfoModel.job_list[0];
    }
    postContentLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(postLbl), EndY(topLbl), WIDTH-60*Scale-postWidth, 40*Scale) text:[NSString stringWithFormat:@"%@  %@", ((tempModel.title&&tempModel.title.length)?tempModel.title:((self.job_cn&&self.job_cn.length)?self.job_cn:@"")), ((tempModel.wage_cn&&tempModel.wage_cn.length)?tempModel.wage_cn:((self.wage_cn&&self.wage_cn.length)?self.wage_cn:@""))] textColor:THEMECOLOR_OF_ORANGE font:Font_18];
    [bottomScrollView addSubview:postContentLbl];
    UIImageView *jiantouImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH-37*Scale, EndY(topLbl)+13.5*Scale, 7*Scale, 13*Scale) imageName:@"jiantou_right"];
    jiantouImgView.userInteractionEnabled = NO;
    [bottomScrollView addSubview:jiantouImgView];
    UIView *postLine = [UIToolsControl initViewWithFrame:CGRectMake(30*Scale, EndY(postLbl), WIDTH-60*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    [bottomScrollView addSubview:postLine];
    
    UIButton *jobBtn = [UIToolsControl initButtonWithFrame:CGRectMake(30*Scale, EndY(topLbl), WIDTH-60*Scale, 40*Scale) imageName:nil target:self action:@selector(jobBtnTap) title:nil titleColor:nil];
    [bottomScrollView addSubview:jobBtn];
    
    dateTimeCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(postLine), WIDTH-60*Scale, PSCellHeight) title:@"面试日期" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    dateTimeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.dateTimePickerView.hidden = NO;
    };
    [bottomScrollView addSubview:dateTimeCell];
    
    timeCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(dateTimeCell), WIDTH-60*Scale, PSCellHeight) title:@"面试时间" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    timeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.timePickerView.hidden = NO;
    };
    [bottomScrollView addSubview:timeCell];
    
    contactCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(timeCell), WIDTH-60*Scale, PSCellHeight) title:@"联系人" text:((interviewBaseinfoModel.contact&&interviewBaseinfoModel.contact.length)?interviewBaseinfoModel.contact:@"") placeholder:@"请填写" cellType:PSCellTypeOfIndicator];
    contactCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf pushInfoEditVC:InfoEditTypeOfName];
    };
    [bottomScrollView addSubview:contactCell];
    
    phoneCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(contactCell), WIDTH-60*Scale, PSCellHeight) title:@"联系电话" text:((interviewBaseinfoModel.telephone&&interviewBaseinfoModel.telephone.length)?interviewBaseinfoModel.telephone:@"") placeholder:@"请填写" cellType:PSCellTypeOfIndicator];
    phoneCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf pushInfoEditVC:InfoEditTypeOfPhoneNumber];
    };
    [bottomScrollView addSubview:phoneCell];
    
    addressCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(phoneCell), WIDTH-60*Scale, PSCellHeight) title:@"面试地址" text:((interviewBaseinfoModel.address&&interviewBaseinfoModel.address.length)?interviewBaseinfoModel.address:@"") placeholder:@"请填写" cellType:PSCellTypeOfIndicator];
    addressCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf pushInfoEditVC:InfoEditTypeOfCompanyAddress];
    };
    [bottomScrollView addSubview:addressCell];
    
    notesCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(addressCell), WIDTH-60*Scale, PSCellHeight) title:@"备注" text:nil placeholder:@"输入面试提醒内容(选填)" cellType:PSCellTypeOfIndicator];
    notesCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf pushInfoEditVC:InfoEditTypeOfCompanyNotes];
    };
    [bottomScrollView addSubview:notesCell];
    
    UILabel *bottomLbl = [UIToolsControl initLabelWithFrame:CGRectMake(30*Scale, EndY(notesCell), WIDTH-60*Scale, 50*Scale) text:@"同时开启手机短信通知" textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_15];
    [bottomScrollView addSubview:bottomLbl];
    switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH-75*Scale, EndY(notesCell)+15*Scale, 45*Scale, 22*Scale)];
    switchBtn.thumbTintColor = COLOR_OF_WHITE;
    switchBtn.onTintColor = THEMECOLOR_OF_ORANGE;
//    switchBtn.on = [infoModel.status integerValue];
    [switchBtn addTarget:self action:@selector(switchBtnTap:) forControlEvents:UIControlEventValueChanged];
    switchBtn.transform = CGAffineTransformMakeScale(0.75, 0.75);
    [bottomScrollView addSubview:switchBtn];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(40*Scale, EndY(bottomLbl)+50*Scale, WIDTH-80*Scale, 46*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"发送邀请面试" titleColor:COLOR_OF_WHITE];
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.layer.cornerRadius = 23*Scale;
    okBtn.layer.masksToBounds = YES;
    okBtn.titleLabel.font = Font(Font_18);
    [bottomScrollView addSubview:okBtn];
    
    bottomScrollView.contentSize = CGSizeMake(0, EndY(okBtn)+20*Scale);
}
- (void)jobBtnTap {
    if (interviewBaseinfoModel.job_list.count) {
        InviteReexamineSelectJobViewController *IRSJVC = [[InviteReexamineSelectJobViewController alloc] init];
        IRSJVC.dataArray = [NSMutableArray arrayWithCapacity:0];
        for (InterviewBaseinfoJobModel *model in interviewBaseinfoModel.job_list) {
            [IRSJVC.dataArray addObject:[model copy]];
        }
        __weak __typeof(self)weakSelf = self;
        IRSJVC.select = ^(NSString * _Nullable jobId, NSString * _Nullable jobTitle, NSString * _Nullable wageCN) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            strongSelf.job_id = jobId;
            self->postContentLbl.text = [NSString stringWithFormat:@"%@  %@", jobTitle, wageCN];
        };
        [self.navigationController pushViewController:IRSJVC animated:YES];
    }
}
- (void)okBtnTap {
    if (dateTimeCell.textField.text.length) {
        if (timeCell.textField.text.length) {
            if (contactCell.textField.text.length) {
                if (phoneCell.textField.text.length) {
                    if (addressCell.textField.text.length) {
                        [self upData];
                    } else {
                        SVPHUDError(@"请填写面试地址");
                    }
                } else {
                    SVPHUDError(@"请填写联系电话");
                }
            } else {
                SVPHUDError(@"请填写联系人");
            }
        } else {
            SVPHUDError(@"请选择面试时间");
        }
    } else {
        SVPHUDError(@"请选择面试日期");
    }
}
- (void)upData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:self.user_id forKey:@"userid"];
    [dic setObject:(self.apply_id?self.apply_id:@"") forKey:@"apply_id"];
    [dic setObject:self.job_id forKey:@"job_id"];
    [dic setObject:dateTimeCell.textField.text forKey:@"interview_date"];
    [dic setObject:timeCell.textField.text forKey:@"interview_time"];
    [dic setObject:contactCell.textField.text forKey:@"contact"];
    [dic setObject:phoneCell.textField.text forKey:@"telephone"];
    [dic setObject:addressCell.textField.text forKey:@"address"];
    [dic setObject:notesCell.textField.text.length?notesCell.textField.text:@"" forKey:@"notes"];
    [dic setObject:(switchBtn.on?@"1":@"0") forKey:@"sms_notice"];
    
    
    [BaseNetManager requestWithMethod:POST path:Company_SendInterview_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
            self.invite(YES);
            [self performSelector:@selector(delayClick) withObject:nil afterDelay:0.5];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)delayClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)pushInfoEditVC:(InfoEditType)type {
    MineInfoEditViewController *MIEVC = [[MineInfoEditViewController alloc] init];
    MIEVC.type = type;
    MIEVC.delegate = self;
    switch (type) {
        case InfoEditTypeOfName: {
            if (contactCell.textField.text) {
                MIEVC.text = contactCell.textField.text;
            }
        } break;
        case InfoEditTypeOfPhoneNumber: {
            if (phoneCell.textField.text) {
                MIEVC.text = phoneCell.textField.text;
            }
        } break;
        case InfoEditTypeOfCompanyAddress: {
            if (addressCell.textField.text) {
                MIEVC.text = addressCell.textField.text;
            }
        } break;
        case InfoEditTypeOfCompanyNotes: {
            if (notesCell.textField.text) {
                MIEVC.text = notesCell.textField.text;
            }
        } break;
        default:
            break;
    }
    [self.navigationController pushViewController:MIEVC animated:YES];
}
- (void)mineInfoEditViewController:(NSString *)info type:(InfoEditType)type {
    switch (type) {
        case InfoEditTypeOfName: {
            contactCell.textField.text = info;
        } break;
        case InfoEditTypeOfPhoneNumber: {
            phoneCell.textField.text = info;
        } break;
        case InfoEditTypeOfCompanyAddress: {
            addressCell.textField.text = info;
        } break;
        case InfoEditTypeOfCompanyNotes: {
            notesCell.textField.text = info;
        } break;
        default:
            break;
    }
}
- (void)switchBtnTap:(UISwitch *)switchButton {
    NSLog(@"switchBtn:%@", switchButton.on?@"YES":@"NO");
}
- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.timePickerView) {
        timeCell.textField.text = model.name;
    }
}
- (void)timeSelectPickerViewCancel:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
}
- (void)timeSelectPickerViewOK:(NSString *)selectText timeSelectPickerView:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
    if (timeSelectPickerView == self.dateTimePickerView) {
        dateTimeCell.textField.text = selectText;
    }
}
- (void)loadDataForTime {
    timeDataArray = [NSMutableArray arrayWithCapacity:0];
    
    NSArray *arr = @[@"08:00", @"08:30", @"09:00", @"09:30", @"10:00", @"10:30", @"11:00", @"11:30", @"12:00", @"12:30", @"13:00", @"13:30", @"14:00", @"14:30", @"15:00", @"15:30", @"16:00", @"16:30", @"17:00", @"17:30", @"18:00", @"18:30", @"19:00", @"19:30", @"20:00"];
    for (NSInteger i = 0; i < arr.count; i++) {
        PublicCategoryElementModel *model = [[PublicCategoryElementModel alloc] init];
        model.name = arr[i];
        [timeDataArray addObject:model];
    }
}

@end
